package com.github.shiqyue.timedelay;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/***
 * 由wwy创建于2018/5/30
 */
@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class);
        StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
        TimeDelayManager timeDelayCenter = new RedisTimeDelayManager(template);
        Thread orderProducter = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                timeDelayCenter.addTask("order", i + "", 5L, TimeUnit.SECONDS);
                System.out.println("生成了订单:" + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        orderProducter.start();
        TimeDelayConsumer timeDelayConsumer = new OrderConsumer();
        timeDelayCenter.addConsumer(timeDelayConsumer);
        timeDelayCenter.consume();
        timeDelayCenter.consume();
        timeDelayCenter.consume();
        timeDelayCenter.consume();
        timeDelayCenter.consume();
    }


}
